package serverapp;



import java.io.*;
import java.net.*;

public class Client
{
	private static String result[][];
	private static boolean connected = false;
	private static int attemptCount = 1;
	// STEP 1 a
	private static Socket server;
	private static ObjectOutputStream out;
	private static ObjectInputStream in;
    
    public static void connectToServer()
    {
    	try
    	{
    		server = new Socket("127.0.0.1", 12345);
	    	// STEP 2 a
	 		out = new ObjectOutputStream(server.getOutputStream());
	          
			// STEP 2 b
	  		in = new ObjectInputStream(server.getInputStream());
	  		connected = true;
	  		System.out.println("\nClient: connection established to server.\nClient: waiting for client to send request...");
	  		attemptCount = 1;
    	}
    	catch (IOException ioe)
        {
            System.out.print(".");
            attemptCount++;
        }
    }
    
    public static void closeConnection()
    {
    	try
		{
			out.close();
			in.close();
			server.close();	
		}
		catch(IOException error)
		{
			System.out.println("Could not close one of the connection");
		}
    }
    
    /** Creates a new instance of ClientApp */
    public static void Start()
    {
        // STEP 1 b
		// Create socket TO SERVER
		String msg = "Client: attempting to connect to server...";
    	System.out.print(msg);
		try
		{
			connected = false;
    		while (connected == false)
    		{
    			// Try to connect
    			connectToServer();
    			if (connected == false)
    			{
    				Thread.sleep(100);
    			}    			
    		}
		}
		catch (Exception e)
		{
			System.out.println("Error in Initial Connect.");
		}
    }

    public static String sendQuery(String message)
    {
    	// STEP 3 a COMMUNICATE  OUTWARDS
        result = new String[1][1];
        try
        {
        	System.out.println("Client says: " + message);
        	out.writeObject(message);
        	out.flush();
			// STEP 3 b COMMUNICATE RECEIVING INWARDS

    	    result = (String[][])in.readObject();
    	    
    	    System.out.println("Client: starting to receive from server...\n");
    	    
    	    for (int Row = 0; Row < result.length; Row++)
			{
				for (int Col = 0; Col < result[Row].length; Col++)
				{
					System.out.print(result[Row][Col] + " ");
				}
				System.out.println();
			}
			
			System.out.println("\nClient: finished receiving from server.");
    	}
    	catch(ClassNotFoundException cnfe)
    	{
    		//lblConnectionStatus.setText("Class not found for casting received object");
    	}
    	catch(IOException ioe)
    	{
    		//lblConnectionStatus.setText("Error in data transfer (sendQuery method)");
    		reConnect();    		
    	}
    	catch (Exception sqle)
    	{
    		System.out.println("Error in sending message: ??? " + sqle.getMessage());
    	}
    	return "transmission successfull";
    }
    
    public static void reConnect()
    {
    	try
    	{
	    	System.out.println("Client: connection was lost. Trying to reconnect...");
	    	closeConnection();
	    	Thread.sleep(100);
	    	connected = false;
	    	while (connected == false)
	    	{
	    		connectToServer();
	    		Thread.sleep(200);
	    	}
    	}
    	catch (Exception e)
    	{
    		System.out.println("Error inside reConnect() paragraph");
    	}
    }
    
    public static String[][] getResult()
    {
    	return result;
    }
    
    public static String getResult(int row, int col)
    {
    	return result[row][col];
    }
}